package com.yoomiito.app.widget.dialog;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import androidx.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding3.view.RxView;
import com.yoomiito.app.R;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.widget.SelectView;

import java.util.concurrent.TimeUnit;

public class OneTypesOfPayDialog extends BaseDialog implements View.OnClickListener {
    private final int MONEY_PAY = 1;
    private final int WX_PAY = 2;
    private final int ALI_PAY = 3;
    private final int MONEY_WX_PAY = 4;
    private final int MONEY_ALI_PAY = 5;

    private OnResultListener mOnResultListener;

    private TextView mPayMoneyTv;
    private TextView mBalanceTv;
    private SelectView mPayMoneySv;
    private ImageView mIconPayMoneyIv;
    private TextView mIconPayMoneyTitleTv;
    private SelectView mPayWXSv;
    private SelectView mPayAliSv;
    private String balance = "";//余额
    private String payMoney = "";//待支付
    private boolean radio = true;//
    private boolean isMoneyPay = true;
    private LinearLayout mPayMoneyLl;
    private boolean showPayMoney = true;
    private boolean installWX;
    private TextView mPayBtn;
    public OneTypesOfPayDialog(@NonNull Context context) {
        this(context, 0);
    }

    public OneTypesOfPayDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    @Override
    public View setView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_pay, null, false);
        ImageView closeIv = view.findViewById(R.id.close);
//        closeIv.setOnClickListener(this);
        RxView.clicks(closeIv)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(unit -> {
                    dismiss();
                    setResult(true);
                });
        mPayBtn = view.findViewById(R.id.pay);
        mPayBtn.setOnClickListener(this);
        view.findViewById(R.id.wx_pay_ll).setVisibility(View.GONE);
        view.findViewById(R.id.aLiPayLl).setVisibility(View.GONE);

        mPayMoneyTv = view.findViewById(R.id.pay_money);
        mBalanceTv = view.findViewById(R.id.useing_remain);
        mBalanceTv.setText("(剩余¥" + balance + ")");
        mPayMoneyLl = view.findViewById(R.id.pay_money_ll);
        mPayMoneyLl.setVisibility(showPayMoney ? View.VISIBLE : View.GONE);
        mIconPayMoneyIv = view.findViewById(R.id.icon_money_pay);
        mIconPayMoneyTitleTv = view.findViewById(R.id.icon_money_pay_title);
        mPayMoneySv = view.findViewById(R.id.item_pay_money);
        setUnClickMoneyPay();
        mPayMoneyTv.setText(payMoney);
        mPayMoneySv.setChangeListener((view1, isSelected) -> {
            changeWay(MONEY_PAY, isSelected);
        });
        setWidthPercent(1);
        setmHeightPercent(0.55);
        setmGravity(Gravity.BOTTOM);
        setDefaultSelect();
        return view;
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay:
                if (AllUtils.isFastClick()) {
                    return;
                }
                setResult(false);
                break;
        }

    }

    private void setResult(boolean dismiss) {
        if (mOnResultListener != null) {
            mOnResultListener.onResult(mPayMoneySv.isSelected() ? 1 : 0, dismiss);
        }
    }

    //单选
    public void setRadio(boolean radio) {
        this.radio = radio;
        setDefaultSelect();
    }

    private void changeWay(int payType, boolean selected) {
        if (radio) {//单选
            if (isMoneyPay) mPayMoneySv.setSelect(payType == MONEY_PAY ? true : false);
        } else {//混选
            if (payType == MONEY_PAY) {
                return;
            }
        }
    }

    public void setOnResultListener(OnResultListener onResultListener) {
        mOnResultListener = onResultListener;
    }

    public interface OnResultListener {
        void onResult(int moneyPay, boolean dismiss);
    }

    public void setBalance(String balance) {
        this.balance = balance;
        if (mBalanceTv != null) mBalanceTv.setText("(剩余¥" + balance + ")");
        setUnClickMoneyPay();
    }

    public void setPayMoney(String payMoney) {
        this.payMoney = payMoney;
        if (mPayMoneyTv != null) mPayMoneyTv.setText(payMoney);
    }

    private void setDefaultSelect() {
//        if (radio && mPayMoneySv != null) {//余额足够
//            mPayMoneySv.setSelect(true);
//        } else if (!radio && mPayMoneySv != null) {
//            if (isMoneyPay) mPayMoneySv.setSelect(true);
//        }
    }

    private void setUnClickMoneyPay() {
        if (mPayMoneySv != null) {

            if (AllUtils.moneyCompare(balance,payMoney)) {
                isMoneyPay = true;
                mPayMoneySv.setEnabled(true);
                mPayMoneySv.setSelect(true);
                mIconPayMoneyTitleTv.setEnabled(true);
                mIconPayMoneyIv.setEnabled(true);
                GradientDrawable drawable = (GradientDrawable) mPayBtn.getBackground();
                drawable.setColor(ResUtil.getColor(R.color.color_orange));
                mPayBtn.setEnabled(true);
            } else {
                isMoneyPay = false;
                mPayMoneySv.setUnClickSelect(R.drawable.icon_unselect_gray);
                mIconPayMoneyTitleTv.setEnabled(false);
                mIconPayMoneyIv.setEnabled(false);
                GradientDrawable drawable = (GradientDrawable) mPayBtn.getBackground();
                drawable.setColor(ResUtil.getColor(R.color.color_orange_80));
                mPayBtn.setEnabled(false);
            }
        }
    }

    public void hideMoneyPay() {
        showPayMoney = false;
    }
}
